mod hf {
    // 默认为私有可见
    fn private_function() {
        println!("called: hf::private_function()");
    }

    // 使用 pub, 公用可见
    pub fn function() {
        println!("called: hf::function()");
    }

    // 同一模块中,可访问所有函数(公用私有均可)
    pub fn indirect_access() {
        println!("called: hf::indirect_access");
        private_function();
    }

    // 模块嵌套
    pub mod nested {
        pub fn function() {
            println!("called: hf::nested::function()");
        }

        #[allow(dead_code)]
        fn private_function() {
            println!("called: hf::nested::private_function()");
        }

        // 使用 pub(in path) 语法定义的函数, 只在特定路径中可见
        // path 必须是父模块或祖先模块
        pub(in crate::hf) fn public_function_in_my_mod() {
            println!("called: hf::nested::public_function_in_my_mod()");
            public_function_in_nested();
        }

        // 使用 pub(self) 语法定义的函数,只在当前模块可见
        pub(self) fn public_function_in_nested() {
            println!("called: hf::nested::public_function_in_nested()");
        }

        // 使用 pub(super) 语法定义的函数只在父模块可见
        pub(super) fn public_function_in_super_mod() {
            println!("called: hf::nested::public_function_in_super_mod()");
        }
    }

    pub fn call_public_function_in_my_mod() {
        println!("called: hf::call_public_function_in_my_mod()");
        nested::public_function_in_my_mod();
        print!("> ");
        nested::public_function_in_super_mod();
    }

    // pub(crate) 使得函数只在当前 crate 可见
    pub(crate) fn public_function_in_crate() {
        println!("called: hf::public_function_in_crate()");
    }

    // 嵌套模块的可见性遵循相同的规则
    mod private_nested {
        #[allow(dead_code)]
        pub fn function() {
            println!("called: hf::private_nested::function()");
        }
    }
}

fn function() {
    println!("called `function()`");
}

fn main() {
    println!("模块->可见性");
    // 模块机制消除了相同名字项之间的歧义
    function();
    hf::function();

    // 公有项,包括嵌套模块内的,都可以在父模块外访问
    hf::indirect_access();
    hf::nested::function();
    hf::call_public_function_in_my_mod();

    // pub(crate) 项可在同一 crate 中任何地方访问
    hf::public_function_in_crate();

    // pub(in path) 只能在指定模块中访问
    // hf::nested::private_function(); // private function
    // hf::nested::public_function_in_my_mod(); // private function

    // 模块的私有项不能直接访问, 即使它是嵌套在公有模块内部的
}
